package _02array;

public class _04DeleteUnsortArray {
    public static void main(String[] args) {
        int[] arr = new int[] {10, 8, 30, 40, 20};
        int size = arr.length;

        // delete 40
        size = deleteKye(arr, size, 40);

        System.out.println("After delete:");
        for (int i = 0; i < size; i++) {
            System.out.println(arr[i]);
        }

    }

    // delete
    // time complexity: O(n)
    // space complexity: O(1)
    private static int deleteKye(int[] arr, int size, int key) {
        // find index
        int index = findKey(arr, size, key);
        
        // not found
        if(index == -1) {
            System.out.println("Element not found.");
            return size;
        }

        // move element
        for (int i = index; i < size - 1 ; i++)
            arr[i] = arr[i + 1];

        return size - 1;
    }

    // search
    // time complexity: O(n)
    // space complexity: O(1)
    private static int findKey(int[] arr, int size, int key) {
        for (int i = 0; i < size; i++) {
            if(arr[i] == key)
                return i;
        }
        return -1;
    }
}
